/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package hu.rsc.feeds.client.gui;

import hu.rsc.feeds.client.Feeds;
import hu.rsc.feeds.client.data.Entry;
import hu.rsc.feeds.client.data.Feed;
import hu.rsc.feeds.client.data.UserCategory;
import hu.rsc.feeds.client.data.UserFeed;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.logical.shared.CloseEvent;
import com.google.gwt.event.logical.shared.CloseHandler;
import com.google.gwt.event.logical.shared.OpenEvent;
import com.google.gwt.event.logical.shared.OpenHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.DisclosurePanel;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.ScrollPanel;
import com.google.gwt.user.client.ui.VerticalPanel;

/**
 * 
 * @author arbuckle
 */
public class RssWidget extends Composite implements CountdownListener {
    Feeds application;
    UserFeed userFeeds;
    VerticalPanel main = new VerticalPanel();
    DisclosurePanel dp;
    VerticalPanel vp = new VerticalPanel();
    public Image moveImg = new Image("images/rss.png");
    public Image delImg = new Image("images/delete.png");
    private Countdown countdown = new Countdown();

    public static final int WIDTH = 300;
    public static final int HEIGHT = 500;

    private Entry[] entries = null;
    private List<Entry> prevEntries = new ArrayList<Entry>();

    HashMap<Entry, PostWidget> widgets = new HashMap<Entry, PostWidget>();

    public RssWidget(final Feeds application, final UserFeed userFeed) {
	this.application = application;
	this.userFeeds = userFeed;

	this.vp.addStyleName("rssWidget");
	this.vp.setSpacing(2);
	HorizontalPanel controlPanel = new HorizontalPanel();
	this.moveImg.addStyleName("rssicons");
	this.moveImg.setTitle("Mozgatás");

	controlPanel.add(this.moveImg);

	this.delImg.addStyleName("rssicons");
	this.delImg.setTitle("Eltávolítás");
	this.delImg.addClickHandler(new ClickHandler() {

	    @Override
	    public void onClick(ClickEvent event) {
		application.removeUserFeed(userFeed);
	    }
	});

	if (Feeds.isLoggined()) {
	    controlPanel.add(this.delImg);
	}
	this.main.add(controlPanel);

	this.countdown.addListener(this);
	controlPanel.add(this.countdown);

	Feed c = application.getFeedById(userFeed.getFeed());
	this.dp = new DisclosurePanel(c.getLabel());
	ScrollPanel scroller = new ScrollPanel();
	scroller.setPixelSize(WIDTH, HEIGHT);
	scroller.add(this.vp);
	this.dp.add(scroller);

	this.main.add(this.dp);
	initWidget(this.main);

	this.dp.addOpenHandler(new OpenHandler<DisclosurePanel>() {

	    @Override
	    public void onOpen(OpenEvent<DisclosurePanel> event) {
		open();
		RssWidget.this.userFeeds.setOpened(true);
	    }
	});
	this.dp.addCloseHandler(new CloseHandler<DisclosurePanel>() {

	    @Override
	    public void onClose(CloseEvent<DisclosurePanel> event) {
		RssWidget.this.userFeeds.setOpened(false);
		RssWidget.this.countdown.stop();
	    }
	});

	if (userFeed.getOpened() != null && userFeed.getOpened()) {
	    this.dp.setOpen(true);
	}
    }

    public UserFeed getUserFeed() {
	return this.userFeeds;
    }

    @Override
    public void onCountdownEnd() {
	if (this.dp.isOpen()) {
	    open();
	}
    }

    public void setPosition(int x, int y) {
	this.userFeeds.setX(x);
	this.userFeeds.setY(y);
    }

    public void setUserCategory(UserCategory uc) {
	this.userFeeds.setUsercategory(uc != null ? uc.getId() : null);
    }

    private void checkNewPosts() {
	if (this.prevEntries.isEmpty()) {
	    return;
	}
	for (Entry key : this.widgets.keySet()) {
	    if (!this.prevEntries.contains(key)) {
		this.widgets.get(key).startBlinking();
	    }
	}
    }

    private void open() {
	this.countdown.start();
	AsyncCallback<Entry[]> callback = new AsyncCallback<Entry[]>() {

	    @Override
	    public void onFailure(Throwable caught) {
		caught.printStackTrace();
	    }

	    @Override
	    public void onSuccess(Entry[] result) {
		refresh(result);
	    }
	};
	Feed c = this.application.getFeedById(this.userFeeds.getFeed());
	this.application.service.getEntries(c.getLabel(), callback);
    }

    private void refresh(Entry[] entries) {
	this.prevEntries.clear();
	if (this.entries != null) {
	    for (Entry e : this.entries) {
		this.prevEntries.add(e);
	    }
	}

	this.entries = entries;
	this.widgets.clear();
	Feed c = this.application.getFeedById(this.userFeeds.getFeed());
	this.vp.clear();
	for (Entry entry : entries) {
	    PostWidget w = new PostWidget(entry, c.getLabel());
	    this.vp.add(w);
	    this.widgets.put(entry, w);
	}

	checkNewPosts();
    }
}
